<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform @switchChange = 'switchChange' ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData,
		getFormFields
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				biddingCache:'mar-jd-biddingStart-title2-bidding-cache',
				salesInstId:'',
				salesUserId:'',
				productCategoryCache:'mar-jd-biddingStart-title2-productCategory-cache',
				biddingCompanyCache:'mar-jd-biddingStart-title2-biddingCompany-cache',
				provideInstCache:'mar-jd-biddingStart-title2-provideInst-cache',
			})
		},
		watch:{
			'formdata.winRate':function(newVal,oldVal){
				let _that =  this
				let num = new Number(newVal)
				if(Number(newVal) > 100){
					num = new Number(100)
					_that.formdata.winRate = num
					_that.$refs.autoform.formdata.winRate = num
				}
				if(_that.$refs.autoform.formdata.purchaseAmount){
						_that.$refs.autoform.formdata.expectedWinningAmount = ((Number(_that.formdata.winRate) / 100) * Number(_that.$refs.autoform.formdata.purchaseAmount)).toFixed(2)
					}
				},
			'formdata.purchaseAmount':function(newVal,oldVal){
				let _that =  this
				if(_that.$refs.autoform.formdata.winRate){
					_that.$refs.autoform.formdata.expectedWinningAmount = (Number(_that.$refs.autoform.formdata.purchaseAmount) * (Number(_that.formdata.winRate) / 100)).toFixed(2)
				}
			}
		},
		methods:{
			switchChange(){
				let _that = this
				if(_that.$refs.autoform.formdata.biddingCompanyId){
					_that.$refs.autoform.clear("title2","biddingCompanyId")
				}
			},
			verifyBiddingCompany(){
				let _that = this
				if(_that.$refs.autoform.formdata.biddingFlag){
					let title = _that.$refs.autoform.getEditorTitle("title2","biddingCompanyId")
					_that.om.toast('公司中标无需选择其他' + title)
					return false
				}else{
					return true
				}
			},
			clearPurchaseAmount(){
				let _that = this
				_that.$refs.autoform.clear("title2","purchaseAmount")
				_that.$refs.autoform.clear("title2","expectedWinningAmount")
			},
			clearwinRate(){
				let _that = this
				_that.$refs.autoform.clear("title2","winRate")
				_that.$refs.autoform.clear("title2","expectedWinningAmount")
			},
			onSelectProjectPhase(item){
				let _that = this
				_that.$refs.autoform.formdata.winRate = item.referNum
			},
			clearBiddingProject(){
				let _that = this
				_that.$refs.autoform.clearGroup("title2")
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						"tsysdict/findChildrenByCodeNoIsdel",
						{
							code:'39127'
						}
					),
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId: '283',
							categoryId:'6431'
						}
					),
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId: '289',
							categoryId:'7222'
						}
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("title2","projectPhaseId","data",configSelectData(res[0],"id",'dicname'))
					_that.$refs.autoform.initSelectIndex("title2","projectPhaseId")
					_that.$refs.autoform.changeEditor("title2","isCloseId","data",configSelectData(res[1],"id",'text'))
					_that.$refs.autoform.initSelectIndex("title2","isCloseId")
					_that.$refs.autoform.changeEditor("title2","overFlagId","data",configSelectData(res[1],"id",'text'))
					_that.$refs.autoform.initSelectIndex("title2","overFlagId")
					_that.$refs.autoform.changeEditor("title2","stateId","data",configSelectData(res[2],"id",'text'))
					_that.$refs.autoform.initSelectIndex("title2","stateId")
				})
			},
			
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					_that.salesInstId = e.salesInstId
					_that.salesUserId = e.salesUserId
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor("title2","biddingId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({
										salesInstId: _that.salesInstId,
										salesUserId:_that.salesUserId,
										firstId: _that.formdata.biddingId?_that.formdata.biddingId:'',
										closeFlag: true
									}),
									url:'biddingStart/getBidding',
									namefield:'biddingProjectName',
									notefield:'biddingCode',
									initload:true,
									searchfield:'q',
									cachename:_that.biddingCache,
								}
							},null,_that.autoformdata)
							changeEditor("title2","productCategoryId",{
								"linkParam":{
									other:JSON.stringify({
									}),
									url:'inventoryCategory/getExistingProductLineAssortment',
									namefield:'name',
									initload:true,
									cachename:_that.productCategoryCache,
								}
							},null,_that.autoformdata)
							changeEditor("title2","provideInstIdStr",{
								"linkParam":{
									other:JSON.stringify({
									}),
									url:'tinstitution/findListWithIgnore',
									namefield:'instname',
									initload:true,
									cachename:_that.provideInstCache,
								}
							},null,_that.autoformdata)
							changeEditor("title2","biddingCompanyId",{
								"linkParam":{
									other:JSON.stringify({
									}),
									url:'biddingStart/getCompete',
									namefield:'competeName',
									searchfield:'q',
									initload:true,
									pagination:true,
									cachename:_that.biddingCompanyCache,
								}
							},null,_that.autoformdata)
							if(_that.formdata.productCategoryId){
								let ids = _that.formdata.productCategoryId.split(",")
								let names = _that.formdata.productCategoryName.split(",")
								let arr = []
								for(let index in ids){
									let obj = {
										id:ids[index],
										name:names[index]
									}
									arr.push(obj)
								}
								changeEditor("title2","productCategoryId",{
									initSelect:arr
								},null,_that.autoformdata)
							}
							if(_that.formdata.provideInstIdStr){
								let ids = _that.formdata.provideInstIdStr.split(",")
								let names = _that.formdata.provideInstNameStr.split(",")
								let arr = []
								for(let index in ids){
									let obj = {
										id:ids[index],
										instname:names[index]
									}
									arr.push(obj)
								}
								changeEditor("title2","provideInstIdStr",{
									initSelect:arr
								},null,_that.autoformdata)
							}
							if(_that.formdata.biddingCompanyId){
								let ids = _that.formdata.biddingCompanyId.split(",")
								let competeNames = _that.formdata.biddingCompanyName.split(",")
								let arr = []
								for(let index in ids){
									let obj = {
										id:ids[index],
										competeName:competeNames[index]
									}
									arr.push(obj)
								}
								changeEditor("title2","biddingCompanyId",{
									initSelect:arr
								},null,_that.autoformdata)
							}
							_that.initload()
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.biddingCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							let fieldArr = getFormFields(_that.autoformdata)
							for(let index in d){
								if(index == 'winRate'){
									continue
								}
								if(index == 'id'){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'biddingId',d['id'])
									_that.$set(_that.formdata,'biddingId',d['id'])
								}
								if(fieldArr.includes(index)){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,d[index])
									_that.$set(_that.formdata,index,d[index])
								}
								if(index == 'purchaseEncode'){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'customerShowEncode',d['purchaseEncode'])
									_that.$set(_that.formdata,'customerShowEncode',d['purchaseEncode'])
								}
								if(index == 'purchaseName'){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'customerShowName',d['purchaseName'])
									_that.$set(_that.formdata,'customerShowName',d['purchaseName'])
								}
								if(index == 'purchaseCode'){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'customerShowCode',d['purchaseCode'])
									_that.$set(_that.formdata,'customerShowCode',d['purchaseCode'])
								}
								if(index == 'customerOwner'){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'customerOwnerName',d['customerOwner'])
									_that.$set(_that.formdata,'customerOwnerName',d['customerOwner'])
								}
								if(index == 'productCategory'){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'productCategoryName',d['productCategory'])
									_that.$set(_that.formdata,'productCategoryName',d['productCategory'])
								}
								if(index == 'winRate'){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'winRate',d['winRate'])
									_that.$set(_that.formdata,'winRate',d['winRate'])
								}
								if(_that.formdata.productCategoryId){
									let ids = _that.formdata.productCategoryId.split(",")
									let names = _that.formdata.productCategoryName.split(",")
									let arr = []
									for(let index in ids){
										let obj = {
											id:ids[index],
											name:names[index]
										}
										arr.push(obj)
									}
									_that.$refs.autoform.changeEditor("title2","productCategoryId",{
										initSelect:arr
									})
								}
								if(_that.formdata.provideInstIdStr){
									let ids = _that.formdata.provideInstIdStr.split(",")
									let names = _that.formdata.provideInstNameStr.split(",")
									let arr = []
									for(let index in ids){
										let obj = {
											id:ids[index],
											instname:names[index]
										}
										arr.push(obj)
									}
									changeEditor("title2","provideInstIdStr",{
										initSelect:arr
									},null,_that.autoformdata)
								}
							}
							_that.$refs.autoform.initSelectIndex("title2","projectPhaseId")
							_that.$refs.autoform.initSelectIndex("title2","isCloseId")
							_that.$refs.autoform.initSelectIndex("title2","overFlagId")
							uni.removeStorage({
								key:_that.biddingCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.productCategoryCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.productCategoryId = d.map(e => e.id).join(",")
							formdata.productCategoryName = d.map(e => e.name).join(",");
							_that.$refs.autoform.changeEditor('title2', 'productCategoryId', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.productCategoryCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.provideInstCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.provideInstIdStr = d.map(e => e.id).join(",")
							formdata.provideInstNameStr = d.map(e => e.instname).join(",");
							_that.$refs.autoform.changeEditor('title2', 'provideInstIdStr', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.provideInstCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.biddingCompanyCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.biddingCompanyId = d.map(e => e.id).join(",")
							formdata.biddingCompanyName = d.map(e => e.competeName).join(",");
							_that.$refs.autoform.changeEditor('title2', 'biddingCompanyId', {
								initSelect: d
							})
							uni.removeStorage({
								key:_that.biddingCompanyCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
